<template>
  <div class="app-container">
    <el-form ref="couponSeriesForm" :model="couponSeriesForm" :rules="couponSeriesRules" label-width="100px">
      <el-form-item label="优惠卷名称:" prop="name">
        <el-input v-model="couponSeriesForm.name" placeholder="请输入优惠卷名称" style="width: 30%" />
      </el-form-item>
      <el-form-item label="优惠金额:" prop="quota">
        <el-input-number v-model="couponSeriesForm.quota" placeholder="请输入优惠金额" :min="0" />
      </el-form-item>
      <el-form-item label="满减金额:" prop="mixLimit">
        <el-input-number v-model="couponSeriesForm.mixLimit" placeholder="请输入满减金额" :min="0" />
      </el-form-item>
      <el-form-item label="日期:" prop="datePicker">
        <el-date-picker
          v-model="couponSeriesDatePicker"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 400px"
          @change="couponSeriesDatePickerChange"
        />
      </el-form-item>
      <el-form-item label="有效期:">
        <el-input-number v-model="couponSeriesForm.effectiveTime" placeholder="请输入有效期" :min="1" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addCouponSeries } from '@/api/couponSeries'

const defaultCouponSeriesForm = {
  name: '基础优惠卷',
  quota: 3,
  mixLimit: 30,
  startDate: null,
  endDate: null,
  effectiveTime: 3
}

export default {
  name: 'CouponSeriesDetail',
  data() {
    const quotaValidator = (rule, value, callback) => {
      if (this.couponSeriesForm.quota > this.couponSeriesForm.mixLimit) {
        callback(new Error('优惠金额需小于满减金额'))
      } else {
        callback()
      }
    }
    const mixLimitValidator = (rule, value, callback) => {
      if (this.couponSeriesForm.quota > this.couponSeriesForm.mixLimit) {
        callback(new Error('满减金额大于优惠金额需'))
      } else {
        callback()
      }
    }
    const datePickerValidator = (rule, value, callback) => {
      if (this.couponSeriesForm.startDate === null && this.couponSeriesForm.endDate === null) {
        callback(new Error('请选择日期'))
      } else {
        callback()
      }
    }
    return {
      couponSeriesForm: Object.assign({}, defaultCouponSeriesForm),
      couponSeriesRules: {
        name: [{ required: true, message: '请输入优惠卷名称', trigger: 'blur' }],
        quota: [{ required: true, trigger: 'blur', validator: quotaValidator }],
        mixLimit: [{ required: true, trigger: 'blur', validator: mixLimitValidator }],
        datePicker: [{ required: true, trigger: 'blur', validator: datePickerValidator }]
      },
      couponSeriesDatePicker: null
    }
  },
  methods: {
    couponSeriesDatePickerChange() {
      if (this.couponSeriesDatePicker !== null) {
        this.couponSeriesForm.startDate = this.couponSeriesDatePicker[0]
        this.couponSeriesForm.endDate = this.couponSeriesDatePicker[1]
      } else {
        this.couponSeriesForm.startDate = null
        this.couponSeriesForm.endDate = null
      }
      // console.info(this.couponSeriesForm.startDate)
      // console.info(this.couponSeriesForm.endDate)
    },
    onSubmit() {
      this.$refs.couponSeriesForm.validate(valid => {
        if (valid) {
          this.$confirm('是否要进行提交操作?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            addCouponSeries(this.couponSeriesForm).then(res => {
              this.$message.success('成功提交')
            })
          })
        } else {
          this.$message.error('未填写完全')
          return false
        }
      })
    }
  }
}
</script>
<style scoped>

</style>
